<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$setting=$this->plugin_setting('setting'.$_SESSION['onemerchid']);
	if(!$setting['leveltype']){
		$leveltype=1;
	}else{
		$leveltype=$setting['leveltype'];
	}
	$type=array('1'=>'ordermoney','2'=>'ordercount','3'=>'downcount');
	$desc=array('1'=>'分销订单总额满（元）','2'=>'分销订单总数满','3'=>'下线总人数满（人）');;
	$tiaojian=$type[$leveltype];
	$des=$desc[$leveltype];
if($_RQ['op']=='list'){
	
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 10;
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
    $fields=array('id','level','levelname','conditions','bonus');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_globonus_level',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['tiaojian']=$this->model()->create_condition($v['conditions']);
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'股东商等级管理'),
			array('op'=>'edit','title'=>'添加等级'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：分红比例（10%）是指：10000分红*10%=100，100元再分配给该等级的所有股东N（100/N=最终客户获得的分红）！','list'=>array(
			array('name'=>'levelname','title'=>'等级名称','type'=>'string'),
			array('name'=>'level','title'=>'等级权重','type'=>'string'),
			array('name'=>'bonus','title'=>'分红比例（%）','type'=>'unitstring','unit'=>'%'),
			array('name'=>'tiaojian','title'=>'升级条件','type'=>'string'),
					
		)),
	);
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
	$item=pdo_get('jy_globonus_level',array('id'=>$id));
	}else{
		$item=array();
	}
	$item['conditions']=$item['conditions']?json_decode($item['conditions'],true):array('order_price'=>'0');
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'分类设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'分销商等级管理'),
			array('op'=>'edit','title'=>'添加等级'),
		),
		'edit'=>array(
			array('name'=>'levelname','title'=>'等级名称','type'=>'string'),
			array('name'=>'level','title'=>'等级权重','type'=>'string','help'=>'越大越高'),
			array('name'=>'bonus','title'=>'分红比例（%）','type'=>'unitstring','unit'=>'%','help'=>'无需填百分号，支持小数后4位'),
		)
	);
	//升级条件表单
	$level_up_types=$this->level_up_types;
	$current_level_up_types=$this->model()->current_level_up_types();
	foreach($current_level_up_types as $type){
		$htmls['edit'][]=array('type'=>'string','name'=>'conditions.'.$type,'title'=>$level_up_types[$type]);
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array_fields(array('levelname','level','bonus','conditions'),$postdata);
	$id=intval($postdata['id']);
	$data['conditions']=$postdata['conditions']?ijson_encode($postdata['conditions']):'';
	if($id){
		pdo_update('jy_globonus_level',$data,array('id'=>$id));
		exi('设置成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$_SESSION['onemerchid'];
		pdo_insert('jy_globonus_level',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_globonus_level',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_globonus_level',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>